package com.xizhao.boot.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * . _________         .__   _____   __
 * ./   _____/__  _  __|__|_/ ____\_/  |_
 * .\_____  \ \ \/ \/ /|  |\   __\ \   __\
 * ./        \ \     / |  | |  |    |  |
 * /_______  /  \/\_/  |__| |__|    |__|
 * .       \/
 * <p>
 * <a href="www.google.com">google</a>
 *
 * @author li tong
 * @description
 * @date 2022/08/18 18:06
 * @see Object
 * @since 1.0
 */
@Aspect // 声明这是切面类
@Component // spring aop 需要注册到spring容器中
public class ConfigLogAspect {

	/**
	 * 定义切点&范围
	 */
	@Pointcut("execution(* com.xizhao.boot.service.WebService.deferredService(..))")
//	@Pointcut("execution(* com.xizhao.boot.service.WebService.*(..))")
//	@Pointcut("execution(* com.xizhao.boot.service..*.*(..))")
//	@Pointcut("execution(* com.xizhao..*.service..*.*(..))")
	public void logPointcut() {
	}

	/**
	 * AOP环绕通知 只能开启一个 否则会多次AOP
	 */
	@Around("logPointcut()")
	public Object around(ProceedingJoinPoint joinPoint) {
		try {
			System.out.println("环绕增强-执行切点前");
			Object res = joinPoint.proceed();
			System.out.println("环绕增强-执行切点后");
			return res;
		} catch (Throwable t) {
			System.out.println("抛出异常" + t);
			return null;
		}
	}

	@Around("execution(* com.xizhao.boot.service.WebService.printHello(..))")
	public Object printHello(ProceedingJoinPoint joinPoint) {
		try {
			System.out.println("环绕增强-执行切点前" + joinPoint.getSignature());
			Object res = joinPoint.proceed();
			System.out.println("环绕增强-执行切点后");
			return res;
		} catch (Throwable t) {
			System.out.println("抛出异常" + t);
			return null;
		}
	}

}
